package util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import model.Account;
import model.Contract;
import model.Lead;
import model.Opportunity;
import model.Rule;
import model.SMSObject;

public class SMSUtil {
	public static String getUserInput(){
		String userInput = null;
		try{
			
			BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
			
			do{
				userInput = br1.readLine();
				if(userInput.trim().length() == 0){
					System.out.println("***Please enter a valid input.***");
				}
				
			}while(userInput.trim().length() == 0);
		}
		catch (IOException e1)
		{
			System.out.println("Exception in username : " + e1);
		}
		return userInput.trim();
	}
	
	public static String formatDate(Date date){
		if(date!=null){
			DateFormat df = new SimpleDateFormat("MM-dd-yy");
			return df.format(date);	
		}
		return null;
	}

	public static void displayLeadHeader(){
		 String format = "%1$-5s|%2$-20s|%3$-15s|%4$-20s|%5$-20s|%6$-10s|%7$-50s|\n";
		System.out.format(format, "Id","Name", "City","Date", "Product Interest", "State", "TBD");
		System.out.println("-----------------------------------------------------------------" +
				"-----------------------------------------------------------------------------------------------------------------------");
	}
	
	public static void displayOpportunityHeader(){
		String format = "%1$-5s|%2$-20s|%3$-15s|%4$-20s|%5$-10s|%6$-15s|%7$-10s||%8$-50s|\n";
		System.out.format(format, "Id", "Name","Description", "Product Name","Quantity","Price", "State","TBD");
		System.out.println("-----------------------------------------------------------------" +
				"------------------------------------------------------------------------------------------------------------------------");
	}
	
	public static void displayContractHeader(){
		System.out.println("CL=Current Level, ML=Maximum Level");
		String format = "%1$-5s|%2$-20s|%3$-15s|%4$-20s|%5$-20s|%6$-40s|%7$-10s|%8$-3s|%9$-3s|%10$-50s|\n";
		System.out.format(format,"Id", "Name", "Cost", "Start date", "End date", "Shipping Address", "State", "CL", "ML", "TBD");
		System.out.println("-----------------------------------------------------------------" +
				"----------------------------------------------------------------------------------------------------------------------------");
	}
	
	
	public static void formatLead(Lead lead){
		//System.out.println("Id\t\tName\t\tCity\t\t\tDate\t\tProduct Interest\t\tState\t\tTBD");
		 String format = "%1$-5s|%2$-20s|%3$-15s|%4$-20s|%5$-20s|%6$-10s|%7$-50s|\n";
		 System.out.format(format, lead.getSmsObjId(),lead.getName(),lead.getCity(),formatDate(lead.getLeadDate()),
				 lead.getProductInterest(), lead.getCurrentState().getStateName(), getTBD(lead));
		 
	}
	
	public static void formatOpportunity(Opportunity opp){
		double qty = 0;
		double price = 0;
		if(opp.getQuote()!=null){
			qty = opp.getQuote().getQuantity();
			price = opp.getQuote().getPrice();
		}
		
		//System.out.println("Id\t\tName\t\tDescription\t\t\tProduct Name\t\tQuantity\t\tPrice\t\tState\t\tTBD");
		 String format = "%1$-5s|%2$-20s|%3$-15s|%4$-20s|%5$-10s|%6$-15s|%7$-10s|%8$-50s|\n";
		 System.out.format(format, opp.getSmsObjId(),opp.getName(),opp.getDescription(),opp.getProductName(),
				 qty,"$"+price, opp.getCurrentState().getStateName(), getTBD(opp));
		 
	}
	
	public static void formatContract(Contract contract){
		//System.out.println("Id\t\tName\t\tCost\t\t\tStart date\t\t\tEnd date\t\tShipping Address\t\t\tState\t\tCurrent Level\t\tMax Level\t\tTBD");
		 String format = "%1$-5s|%2$-20s|%3$-15s|%4$-20s|%5$-20s|%6$-40s|%7$-10s|%8$-3s|%9$-3s|%10$-50s|\n";
		 System.out.format(format, contract.getSmsObjId(),contract.getDescription(),"$"+contract.getCost(), 
				 formatDate(contract.getStartDate()), formatDate(contract.getEndDate()),
				 contract.getShippingAddress(), contract.getCurrentState().getStateName(),
				 contract.getCurrentLevel(), 
				 contract.getMaxLevel(),getTBD(contract));
		 
	}
	
	
	
	
	
	
	public static void displayEnd(){
		System.out.println("-----------------------------------------------------------------" +
		"----------------------------------------------------------------------------------------------------------------------------");
		
	}
	
	public static void displayRuleHeader(){
		
		 String format = "%1$-5s|%2$-20s|%3$-20s|%4$-50s|\n";
		System.out.format(format, "Id","Name", "Type","Description" );
		System.out.println("-----------------------------------------------------------------" +
				"-----------------------------------------------------------------------------");
	}
	
	public static void formatRule(Rule rule){
		//System.out.println("Id\t\tName\t\tDescription\t\t\tType");
		 String format = "%1$-5s|%2$-20s|%3$-20s|%4$-50s|\n";
		 System.out.format(format,rule.getRuleID(), rule.getRuleName(), rule.getRuleType(),rule.getRuleDescripton());		 
	}
	
	public static void displayAccountHeader(){
		
		 String format = "%1$-5s|%2$-20s|\n";
		System.out.format(format, "Id","Name");
		System.out.println("-----------------------------------------------------------------" +
				"-----------------------------------------------------------------------------");
	}
	public static void formatAccount(Account acct){
		//System.out.println("Id\t\tName");
		 String format = "%1$-5s|%2$-20s|\n";
		 System.out.format(format,acct.getAccountId(), acct.getAccountName());		 
	}
	
	
	
	
	public static long dateDifference(Date date1, Date date2){
		Calendar cal1  = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		
		cal1.set(date1.getYear(), date1.getMonth(), date1.getDate());
		cal2.set(date2.getYear(), date2.getMonth(), date2.getDate());
		
		long difference = cal2.getTimeInMillis() - cal1.getTimeInMillis();
		
		long dayDiff = difference / (24 * 60 * 60 * 1000);
		
		return dayDiff;
		
		
	}
	
	public static String getTBD(SMSObject smsObject){
		if(smsObject instanceof Lead){
			if(smsObject.getCurrentState().getStateName().equals("Raw")){
				return "**Import pending**";
			}else if(smsObject.getCurrentState().getStateName().equals("Imported")){
				return "**Lead yet to be assigned**";
			}else if(smsObject.getCurrentState().getStateName().equals("Assigned")){
				return "**Merge pending**";
			}else if(smsObject.getCurrentState().getStateName().equals("Merged")){
				return "**Prepare pending**";
			}else if(smsObject.getCurrentState().getStateName().equals("Prepared")){
				return "**Approval pending**";
			}else if(smsObject.getCurrentState().getStateName().equals("Qualified")){
				return "**Ready to be imported as opportunity**";
			}
		}if(smsObject instanceof Opportunity){
			if(smsObject.getCurrentState().getStateName().equals("Imported")){
				return "**Prepare pending**";
			}else if(smsObject.getCurrentState().getStateName().equals("Prepared")){
				return "**Quote to be created**";
			}else if(smsObject.getCurrentState().getStateName().equals("Quoted")){
				return "**Approval pending**";
			}else if(smsObject.getCurrentState().getStateName().equals("Approved")){
				return "**Ready to be imported as Contract**";
			}
		}if(smsObject instanceof Contract){
			if(smsObject.getCurrentState().getStateName().equals("Imported")){
				return "**Prepare pending**";
			}else if(smsObject.getCurrentState().getStateName().equals("Prepared")){
				return "**Approval pending**";
			}else if(smsObject.getCurrentState().getStateName().equals("Approved")){
				return "**Finalize Pending**";
			}else if(smsObject.getCurrentState().getStateName().equals("Finalized")){
				return "@@@@@@@SMSObject processing Completed!!@@@@@@@@";
			}
		}
		
		return null;
	}
	

	
	
	
	
}
